package com.my.control
{
	import com.my.control.validate.LoginViewValidate;
	import com.my.model.bean.UserInfoEntity;
	import com.my.model.locator.UserInfoModelLocator;
	import com.my.service.RemoteServiceFactory;
	import com.my.view.right.LoginView;
	
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.core.UIComponent;
	import mx.rpc.events.ResultEvent;

	public class LoginViewControl extends AbstractControl implements IControl{
		private static var instance:LoginViewControl = null;

		private var loginValidate:LoginViewValidate;

		public function LoginViewControl(value : UIComponent){
			super(value);
			loginValidate = new LoginViewValidate(value);
		}
		
		public static function startup(value : UIComponent):void{
			instance = new LoginViewControl(value);
		}
		
		public override function addEventListener():void{
			getView().loginBt.addEventListener(MouseEvent.CLICK,loginBtClickHandler);
		}
		
		public function loginBtClickHandler(event:MouseEvent):void
		{
			if (loginValidate.validate())
			{
				RemoteServiceFactory.getInstance().userInfoService.addEventListener(ResultEvent.RESULT, function(e:ResultEvent):void
				{
					RemoteServiceFactory.getInstance().userInfoService.removeEventListener(ResultEvent.RESULT, arguments.callee);
					
					var v : UserInfoEntity = e.result as UserInfoEntity;
					if(v != null){
						getView().loginResult.text = "";
						
							UserInfoModelLocator.getInstance().userInfo = v;
							
							MessageControl.getInstance().rightSwitchToState(1);
					}else{
						getView().loginResult.text = "用户名不存在,或密码错误.";
					}
					
				},false ,0,true);
				RemoteServiceFactory.getInstance().userInfoService.login(getView().username.text,getView().password.text);
			}
		}
		public function getView():LoginView{
			return LoginView(view);
		}
	}
}